
using System;
using Mono.Unix;
using Habazie.kf2.Core;

namespace Habazie.kf2.GUI
{
	
	
	public class StatusButton : Gtk.Button
	{
		///<summary>Image widget</summary>
		private Gtk.Image	m_image;
		
		///<summary>Label widget</summary>
		private Gtk.Label m_label;
		
		///<summary>Arrow widget</summary>
		private Gtk.Arrow m_arrow;
		
		///<summary>Menu</summary>
		private StatusMenu m_menu;
		
		///<summary>Status from the list has been selected</summary>
		public event StatusEventHandler StatusActivated = null;

		public StatusButton() : base ()
		{
			m_image = new Gtk.Image (Gdk.Pixbuf.LoadFromResource ("status.png"));
			m_image.Show ();
			
			m_label = new Gtk.Label (Catalog.GetString ("Status"));
			m_label.Show ();
			
			m_arrow = new Gtk.Arrow (Gtk.ArrowType.Down, Gtk.ShadowType.Out);
			m_arrow.Show ();
			
			Gtk.HBox box = new Gtk.HBox (false, 6);
			
			box.PackStart (m_image, false, false, 0);
			box.PackStart (m_label, true, true, 0);
			box.PackEnd (m_arrow, false, false, 0);
			box.Show ();
			this.Add (box);
			
			m_menu = new StatusMenu ();
			m_menu.StatusActivated += OnMenuActivated;
			int w, h;
			m_menu.GetSizeRequest (out w, out h);
			m_label.SetSizeRequest (w, 1);
			this.Clicked += OnClicked;
		}


		public void SetStatus (jabber.protocol.client.PresenceType presence, string show)
		{
			string text = Util.StatusToString (presence, show);
			m_label.Text = text;
			m_image.Pixbuf = Pixbufs.Get (presence, show);
		}

		protected virtual void OnClicked (object o, EventArgs args)
		{
			m_menu.Popup (null, null, PositionMenu, 0, 0);
		}
		
		protected virtual void PositionMenu (Gtk.Menu menu, out int x, out int y, out bool push_in)
		{
			int wx, wy;
			int width, height;
			Gtk.Widget w = this;
			w.GdkWindow.GetOrigin (out wx, out wy);
			w.GdkWindow.GetSize (out width, out height);
			
			x = wx;
			y = wy + height;
			push_in = true;
		
//		
//			int wx;
//			int wy;
//			int w, h, d;
//			this.GdkWindow.GetGeometry (out wx, out wy, out w, out h, out d);
//			Util.Debug ("GUI", "{0}x{1}+{2}+{3}@{4}", wx, wy, w, h, d);
//			
//			int rx, ry;
//			((Gtk.Window) this.Toplevel).GetPosition (out rx, out ry);
//			x = /*wx + */rx;
//			y = /*wy + */h + ry;
//			push_in = true;
		}
		
		protected virtual void OnMenuActivated (object o, StatusEventArgs args)
		{
			if (StatusActivated != null)
				StatusActivated (this, args);
		}
	}
}
